package edu.tongji.engine.db.dao;

import org.hibernate.criterion.Example;

import edu.tongji.engine.common.Log;
import edu.tongji.engine.db.HibernateTemplate;
import edu.tongji.engine.def.ProcessDefinition;
import edu.tongji.engine.exe.ProcessInstance;

public class ProcessDao implements IProcessDao {

	private HibernateTemplate session;

	public ProcessDao(){
		session = new HibernateTemplate();
	}

	public ProcessDefinition getProcessDefinition(String processName) {
		// TODO Auto-generated method stub
		try{
			ProcessDefinition pd = new ProcessDefinition();
			pd.setName(processName);
			Example process = Example.create(pd);
			return (ProcessDefinition)session.findByCriteriaExample(ProcessDefinition.class, process).get(0);
			}
			catch(Exception e){
				System.out.println(e.toString());
				Log.getLoger().loger.error("save Process Object Fail! ", e);
				return null;
			}

	}



	public boolean saveProcessDefition(ProcessDefinition pd){
		try{
			session.saveOrUpdate(pd);
			return true;
			}
			catch(Exception e){
				System.out.println(e.toString());
				Log.getLoger().loger.error("save Process Object Fail! ", e);
				return false;
			}
	}

	public boolean deleteProcessDefinition(ProcessDefinition pd) {
		// TODO Auto-generated method stub
		try{
			session.delete(pd);
			return true;
			}
			catch(Exception e){
				System.out.println(e.toString());
				Log.getLoger().loger.error("delete Process Object Fail! ", e);
				return false;
			}
	}

	public boolean deleteProcessDefinition(String processName) {
		// TODO Auto-generated method stub
		try{
			ProcessDefinition pd = this.getProcessDefinition(processName);
			this.deleteProcessDefinition(pd);
			return true;
			}
			catch(Exception e){
				System.out.println(e.toString());
				Log.getLoger().loger.error("delete Process Object Fail! ", e);
				return false;
			}
	}

	public boolean deleteProcessInstance(ProcessInstance instance) {
		// TODO Auto-generated method stub
		try{
			session.delete(instance);
			return true;
			}
			catch(Exception e){
				System.out.println(e.toString());
				Log.getLoger().loger.error("delete Process instance Object Fail! ", e);
				return false;
			}
	}

	public boolean deleteProcessInstance(String name) {
		// TODO Auto-generated method stub
		try{
			ProcessInstance pi = this.getProcessInstance(name);
			this.deleteProcessInstance(pi);

			return true;
		}
		catch(Exception e){
			System.out.println(e.toString());
			Log.getLoger().loger.error("delete Process instance Object Fail! ", e);
			return false;
		}
	}

	public ProcessInstance getProcessInstance(String name) {
		// TODO Auto-generated method stub
//		try{
////			StringBuilder hql = new StringBuilder();
////			hql.append("from ProcessInstance where name='");
////			hql.append(name);
////			hql.append("'");
////			String hql = "from ProcessInstance as pi where pi.name='"+name+"'";
////			return (ProcessInstance)session.findByHQL(hql.toString());
//
//
//		}
//		catch(Exception e){
//			return null;
//		}

		try{
			ProcessInstance pi = new ProcessInstance();
			pi.setName(name);
			Example example = Example.create(pi);
			return (ProcessInstance)session.findByCriteriaExample(ProcessInstance.class, example).get(0);
			}
			catch(Exception e){
				System.out.println(e.toString());
				Log.getLoger().loger.error("get Process instance Object Fail! ", e);
				return null;
			}
	}

	public boolean saveProcessInstance(ProcessInstance instance) {
		// TODO Auto-generated method stub
		try{
			session.saveOrUpdate(instance);
			return true;
			}
			catch(Exception e){
				System.out.println(e.toString());
				Log.getLoger().loger.error("save Process instance Object Fail! ", e);
				return false;
			}
	}

	public boolean deleteProcessDefinitionAll(){
		try{
			String processhql = "delete ProcessDefinition";
			String nodehql = "delete Node";
			session.deleteByHQL( nodehql );
			session.deleteByHQL(processhql);
			return true;
		}
		catch(Exception e){
			System.out.println(e.toString());
			Log.getLoger().loger.error("delete all Process instance Object Fail! ", e);
			return false;
		}
	}

	public boolean deleteALlProcessDefinition() {
		try{
			String hql = "delete from ProcessDefinition";
			session.deleteByHQL(hql);
			return true;
			}
			catch(Exception e){
				System.out.println(e.toString());
				Log.getLoger().loger.error("delete all Objects Fail! ", e);
				return false;
			}
	}
}
